package cassys.actions.moduleManagement;

import cassys.actions.core.BaseAction;
import cassys.ejb.clients.ModuleManagementDelegate;
import cassys.domain.classManagement.*;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author User
 */
public class ViewModule extends BaseAction {

    private long moduleId;
    private ModuleType moduleType;
    private String moduleCode;
    private String moduleTitle;
    private Lecture owner;
    private ModuleManagementDelegate moduleDelegate;
    private List<ModuleType> moduleTypes;
    private List<Lecture> lecturers;
    private Collection<Lecture> selectedLectures;

    public ViewModule() {
        moduleDelegate = new ModuleManagementDelegate();
        this.lecturers = moduleDelegate.getAllLecturers();
        this.moduleTypes = moduleDelegate.getModuleTypes();
    }

    @Override
    public String execute() throws Exception {

        this.setModuleId(Long.parseLong(request.getParameter("moduleId")));
         System.out.println(this.moduleId);
        Module existing = moduleDelegate.findByClassGroupId(getModuleId());
        this.moduleCode = existing.getModuleCode();
        this.moduleTitle = existing.getTitle();
        this.moduleType = existing.getType();
        this.owner = existing.getOwner();
        this.selectedLectures = existing.getLectures();
        System.out.println("module view action started");
        return SUCCESS;
    }

    public String updateModule() throws Exception {
        System.out.println(this.moduleId);
        System.out.println(this.moduleTitle);
        System.out.println(selectedLectures.size());
        System.out.println(this.owner.getLectureId());
        moduleDelegate.updateModule(mapModule());
        System.out.println("invoke update module action");

        //moduleDelegate.crateModule(moduleDTO);
        return SUCCESS;
    }

    private Module mapModule() {
        System.out.println(" Owner Name: "+this.owner.getName());
        System.out.println(" Owner Id: "+this.owner.getLectureId());
        Module newModule = new Module(getModuleCode(), getModuleTitle(), getModuleType(), getOwner());
        newModule.setModuleId(this.moduleId);
          System.out.println(" UPD "+newModule.toString());
         System.out.println("sssss"+selectedLectures.size());
        for (Lecture lecture : selectedLectures) {
            newModule.addLecture(lecture);
        }
       
        return newModule;
    }

    public String deleteModule() throws Exception {
        System.out.println("invoke update module action");
        // moduleDelegate.crateModule(moduleDTO);
        return SUCCESS;
    }

    public List<ModuleType> getModuleTypes() {
        return moduleTypes;
    }

    public List<Lecture> getLecturers() {
        return lecturers;
    }

    /**
     * @return the moduleType
     */
    public ModuleType getModuleType() {
        return moduleType;
    }

    /**
     * @param moduleType the moduleType to set
     */
    public void setModuleType(ModuleType moduleType) {
        this.moduleType = moduleType;
    }

    /**
     * @return the moduleCode
     */
    public String getModuleCode() {
        return moduleCode;
    }

    /**
     * @param moduleCode the moduleCode to set
     */
    public void setModuleCode(String moduleCode) {
        this.moduleCode = moduleCode;
    }

    /**
     * @return the moduleTitle
     */
    public String getModuleTitle() {
        return moduleTitle;
    }

    /**
     * @param moduleTitle the moduleTitle to set
     */
    public void setModuleTitle(String moduleTitle) {
        this.moduleTitle = moduleTitle;
    }

    /**
     * @return the owner
     */
    public Lecture getOwner() {
        return owner;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(Lecture owner) {
        this.owner = owner;
    }

    /**
     * @return the selectedLectures
     */
    public Collection<Lecture> getSelectedLectures() {
        return selectedLectures;
    }

    /**
     * @param selectedLectures the selectedLectures to set
     */
    public void setSelectedLectures(Collection<Lecture> selectedLectures) {
        this.selectedLectures = selectedLectures;
    }

    /**
     * @return the moduleId
     */
    public long getModuleId() {
        return moduleId;
    }

    /**
     * @param moduleId the moduleId to set
     */
    public void setModuleId(long moduleId) {
        this.moduleId = moduleId;
    }
}
